package org.phoenix.mall.admin.service.impl;

import org.phoenix.mall.admin.common.pojo.vo.RightVO;
import org.phoenix.mall.admin.common.pojo.vo.RoleVO;
import org.phoenix.mall.admin.mapper.RoleMapper;
import org.phoenix.mall.admin.mapper.RolerightMapper;
import org.phoenix.mall.admin.pojo.Role;
import org.phoenix.mall.admin.service.RoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    private RoleMapper roleMapper;
    private RolerightMapper rolerightMapper;

    @Override
    public Role createRole(Role role) {
        roleMapper.insert(role);
        return role;
    }

    @Override
    public List<RoleVO> getRoleList() {
        List<RoleVO> list = new ArrayList<>();
        List<Role> roleList = roleMapper.selectAll();
        if (roleList != null && !roleList.isEmpty()) {
            RoleVO roleVO;
            for (Role item : roleList) {
                roleVO = new RoleVO();
                roleVO.setId(item.getId());
                roleVO.setRoleName(item.getRoleName());
                roleVO.setRoleDesc(item.getRoleDesc());
                roleVO.setChildren(recursionRight(item.getId(), 0));

                list.add(roleVO);
            }
        }
        return list;
    }

    @Override
    public List<RightVO> grantRoleRights(Integer roleId, String[] rightIds) {

        //删除角色对应的权限
        rolerightMapper.deleteRightsByRid(roleId);

        //分配权限时记录角色与权限的关联记录
        for (String rightId : rightIds) {
            rolerightMapper.insertRoleRight(roleId, Integer.valueOf(rightId));
        }

        //查询角色列表信息并返回
        return recursionRight(roleId, 0);
    }

    @Override
    public List<RightVO> deleteRights(Integer roleId, Integer rightId) {
        recursionDeleteRight(roleId, rightId);
        return recursionRight(roleId, 0);
    }

    @Override
    public List<RightVO> getRightsById(Integer roleId) {
        return rolerightMapper.queryRightsByRoleId(roleId);
    }

    private void recursionDeleteRight(Integer roleId, Integer rightId) {
        rolerightMapper.deleteRightById(roleId, rightId);
        List<RightVO> childRights = rolerightMapper.queryRightsById(roleId, rightId);
        if (childRights != null && !childRights.isEmpty()) {
            for (RightVO item : childRights) {
                recursionDeleteRight(roleId, item.getId());
            }
        }
    }

    private List<RightVO> recursionRight(Integer roleId, Integer pid) {
        List<RightVO> rights = rolerightMapper.queryRightsById(roleId, pid);
        if (rights != null && !rights.isEmpty()) {
            List<RightVO> childRights;
            for (RightVO item : rights) {
                childRights = recursionRight(roleId, item.getId());
                item.setChildren(childRights);
            }
        }

        return rights;
    }

    @Resource
    public void setRoleMapper(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }

    @Resource
    public void setRolerightMapper(RolerightMapper rolerightMapper) {
        this.rolerightMapper = rolerightMapper;
    }
}
